package bTree.LeetCode;

public class 旋转链表 {
    public ListNode rotateRight(ListNode head, int k) {
        if(head==null||k==0){
            return head;
        }
        ListNode p=head;
        ListNode end=null;
        int num=0;
        while (p!=null){
            if(p.next==null){
                end=p;
            }
            num++;
            p=p.next;
        }
        if(num==1){
            return head;
        }
        ListNode Pre;
        ListNode cur;
        k=k%num;
        if(k==0){
            return head;
        }
        int pre=num-k;
        int num2=1;
        ListNode pp=head;
        while (true){
            if(num2==pre){
                Pre=pp;
                cur=Pre.next;
                break;
            }
            num2++;
            pp=pp.next;
        }
        Pre.next=null;
        end.next=head;

        return cur;
    }
}